home *** CD-ROM | disk | FTP | other *** search
- ' This subroutine was originally uploaded by Lyle Jensen. I noticed right
- ' off that it could be more efficient and structured so I spent some time
- ' with it and here is the result. I have left the main routine the same.
- ' Only the sub has been to protect the innocent.
-
- DECLARE FUNCTION BIBO$ (Number$, BaseIn AS INTEGER, BaseOut AS INTEGER)
-
- ' Program to demonstrate the BIBO (base in, base out) function.
- ' This exercise arose out of a need I had to convert phone numbers
- ' (with area code) to an 8 character (or less) string so I could use
- ' them as DOS filenames (without using the 3 character extension---I needed
- ' that for something else). Converting to hex wouldn't quite do it, so
- ' I tried base 20 and it worked. I had the need to convert them back to
- ' valid phone numbers for transmission, hence the other function.
- ' I started with BASECV.BAS downloaded from CIS IBMSYS lib #7. Frankly,
- ' I really haven't had time to tear the algorithms apart to see exactly
- ' how they work, so I cannot address that! The original author (the name
- ' was not in the file, so I cannot credit them), stated that they had
- ' help from the National Bureau of Standards Handbook of Mathematical
- ' Functions.
- ' Comments are welcome (especially how to increase speed), use at your
- ' own risk, etc. I have not tested the results with the NBS or any such
- ' authority, nor have I even tested the outer limits of the algorithms---
- ' which are considerable since I've used double precision. I do know
- ' that it will convert any phone number I've thrown at it to an 8 character
- ' string and back again, so it suits my needs.
- ' Lyle Jensen CIS 76666,1401.
-
- DEFINT A-Z
-
- PRINT
- Seed$ = "18005551212"
- PRINT "Seed: "; Seed$
- Base20$ = BIBO$(Seed$, 10, 20)
- PRINT "Base 10 to 20: "; Base20$
- Base10$ = BIBO$(Base20$, 20, 10)
- PRINT "Back to base 10: "; Base10$
- Seed$ = "FFFF"
- PRINT "New seed: "; Seed$
- Base10$ = BIBO$(Seed$, 16, 10)
- PRINT "Base 16 to 10: "; Base10$
- Base16$ = BIBO$(Base10$, 10, 16)
- PRINT "Back to base 16: "; Base16$
-
- END
-
- FUNCTION BIBO$ (Number$, BaseIn AS INTEGER, BaseOut AS INTEGER)
-
- CONST Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- Decimal# = 0
-
- ' First we convert from BaseIn to base 10
- SELECT CASE BaseIn
- CASE 10
- Decimal# = VAL(Number$)
-
- ' BaseIn is too high
- CASE IS > LEN(Digits)
- EXIT FUNCTION
-
- CASE ELSE
- ' Convert to base 10.
- NumberLength = LEN(Number$)
- Decimal# = 0
-
- FOR I = 1 TO NumberLength
- J = INSTR(Digits, MID$(Number$, I, 1))
- ' Cannot continue if BaseIn is illegal
- IF J = 0 THEN EXIT FUNCTION
- Decimal# = Decimal# + INT((J - 1) * (BaseIn ^ (NumberLength - I)) + .5)
- NEXT I
-
- END SELECT
-
- ' Second we convert from base 10 to BaseOut
- SELECT CASE BaseOut
-
- CASE 8
- BIBO$ = OCT$(Decimal#)
-
- CASE 10
- BIBO$ = STR$(Decimal#)
-
- CASE 16
- BIBO$ = HEX$(Decimal#)
-
- ' BaseOut is too high
- CASE IS > LEN(Digits)
- EXIT FUNCTION
-
- CASE ELSE
- DO
- Y# = Decimal# / BaseOut
- X = INT((Y# - INT(Y#)) * BaseOut + 1.5)
- NumberOut$ = MID$(Digits, X, 1) + NumberOut$
- Decimal# = INT(Y#)
- LOOP WHILE Decimal# > 0
- BIBO$ = NumberOut$
-
- END SELECT
-
- END FUNCTION
-
-